﻿using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using IntelligentEscalator.DAL;
using IntelligentEscalator.Models;

namespace IntelligentEscalator.BUS
{
    public class SpareBUS
    {
        public ResponseMessage AddSpare(SpareInfo spareInfo)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            try
            {
                if (string.IsNullOrEmpty(spareInfo.productionDate))
                {
                    spareInfo.productionDate = "null";
                }
                else
                {
                    spareInfo.productionDate = string.Format("'{0}'", Convert.ToDateTime(spareInfo.productionDate).ToString("yyyy-MM-dd HH:mm:ss"));
                }

                string result = da.AddSpare(spareInfo);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    LogHelper.Log(result);
                    message.errcode = 1;
                    message.errmsg = "新增备件信息失败";
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "新增备件信息异常";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增备件";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("新增备件" + e.ToString());
            }
            return message;
        }

        public ResponseMessage EditSpare(SpareInfo spareInfo)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            if (string.IsNullOrEmpty(spareInfo.productionDate))
            {
                spareInfo.productionDate = "null";
            }
            else
            {
                spareInfo.productionDate = string.Format("'{0}'", Convert.ToDateTime(spareInfo.productionDate).ToString("yyyy-MM-dd HH:mm:ss"));
            }
            string result = da.EditSpare(spareInfo);

            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "修改备件信息失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage DelSpare(int spareid)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            string result = string.Empty;
            try
            {
                result = da.DelSpare(spareid);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除备件";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("删除备件" + e.ToString());
            }
            return message;
        }

        public ResponseMessage BatchDelSpare(string ids)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            string result = string.Empty;
            try
            {
                result = da.BatchDelSpare(ids);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "批量删除备件";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("批量删除备件" + e.ToString());
            }
            return message;
        }

        public ResponseMessage QuerySpare(SpareInfo spareInfo)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            try
            {
                List<SpareInfo> content = da.QuerySpare(spareInfo);

                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = da.QuerySpareConut(spareInfo);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询备件";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询备件" + e.ToString());
            }

            return message;
        }

        public ResponseMessage LoadSpare(int spareid)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            try
            {
                SpareInfo content = da.LoadSpare(spareid);
                if (content != null)
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = content;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "未查询到相关信息";
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "载入备件";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("载入备件" + e.ToString());
            }
            return message;
        }

        public ResponseMessage CleanUpSpare()
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            string result = string.Empty;
            try
            {
                result = da.CleanUpSpare();

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "清除备件信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("清除备件信息" + e.ToString());
            }
            return message;
        }

        public ResponseMessage AddSpareRecord(Spareupdaterecord spareupdaterecord)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            try
            {
                if (string.IsNullOrEmpty(spareupdaterecord.updateDate))
                {
                    spareupdaterecord.updateDate = "null";
                }
                else
                {
                    spareupdaterecord.updateDate = string.Format("'{0}'", Convert.ToDateTime(spareupdaterecord.updateDate).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                string result = da.AddSpareRecord(spareupdaterecord);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    LogHelper.Log(result);
                    message.errcode = 1;
                    message.errmsg = "新增备件更换记录失败";
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                message.errcode = 1;
                message.errmsg = "新增备件更换记录异常";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增备件更换记录";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("新增备件更换记录" + e.ToString());
            }
            return message;
        }

        public ResponseMessage EditSpareRecord(Spareupdaterecord spareupdaterecord)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            if (string.IsNullOrEmpty(spareupdaterecord.creationDate))
            {
                spareupdaterecord.updateDate = "null";
            }
            else
            {
                spareupdaterecord.updateDate = string.Format("'{0}'", Convert.ToDateTime(spareupdaterecord.updateDate).ToString("yyyy-MM-dd HH:mm:ss"));
            }
            string result = da.EditSpareRecord(spareupdaterecord);

            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "修改备件更换记录失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage DelSpareRecord(int spareid)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            string result = string.Empty;
            try
            {
                result = da.DelSpareRecord(spareid);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除备件更换记录";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("删除备件更换记录" + e.ToString());
            }
            return message;
        }

        public ResponseMessage BatchDelSpareRecord(string ids)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            string result = string.Empty;
            try
            {
                result = da.BatchDelSpareRecord(ids);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "批量删除备件更换记录";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("批量删除备件更换记录" + e.ToString());
            }
            return message;
        }

        public ResponseMessage QuerySpareRecord(Spareupdaterecord spareupdaterecord)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            try
            {
                List<Spareupdaterecord> content = da.QuerySpareRecord(spareupdaterecord);

                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = da.QuerySpareRecordConut(spareupdaterecord);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询备件更换记录";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询备件更换记录" + e.ToString());
            }

            return message;
        }

        public ResponseMessage LoadSpareRecord(int spareupdaterecordid)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            try
            {
                Spareupdaterecord content = da.LoadSpareRecord(spareupdaterecordid);
                if (content != null)
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = content;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "未查询到相关信息";
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "载入备件更换记录";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("载入备件更换记录" + e.ToString());
            }
            return message;
        }

        public ResponseMessage CleanUpSpareRecorde()
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            string result = string.Empty;
            try
            {
                result = da.CleanUpSpareRecord();

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "清除备件更换记录";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("清除备件更换记录" + e.ToString());
            }
            return message;
        }

        public ResponseMessage AddSpareElevator(SpareElevator spareElevator)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            string failinfo = string.Empty;
            try
            {
                string[] spare_ids = spareElevator.spareIds.Split(',');
                string[] ele_ids = spareElevator.elevatorIds.Split(',');
                for (int m = 0; m < spare_ids.Length; m++)
                {
                    try
                    {
                        int spareId = Convert.ToInt32(spare_ids[m]);
                        for (int n = 0; n < ele_ids.Length; n++)
                        {
                            try
                            {
                                SpareElevator spareElevator1 = new SpareElevator();
                                spareElevator1.spareId = spareId;
                                spareElevator1.elevatorId = Convert.ToInt32(ele_ids[n]);
                                bool checkresult = da.CheckSpareElevator(spareElevator1);
                                if (!checkresult)
                                {
                                    string result = da.AddSpareElevator(spareElevator1);
                                    if (result.Contains("fail"))
                                    {
                                        failinfo += spareElevator1.spareId + ",";
                                    }
                                }
                            }
                            catch
                            {
                                continue;
                            }
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }

                if (failinfo.Length > 0)
                {
                    message.errcode = 1;
                    message.errmsg = "设备备件绑定失败";
                    message.data = null;
                    message.total = 0;
                }
                else
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "设备绑定备件关系异常";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "设备绑定备件关系";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("设备绑定备件关系" + e.ToString());
            }
            return message;
        }

        public ResponseMessage BatchDelSpareElevator(string ids)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            string result = string.Empty;
            try
            {
                result = da.BatchDelSpareElevator(ids);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "批量删除设备绑定备件关系";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("批量删除设备绑定备件关系" + e.ToString());
            }
            return message;
        }

        public ResponseMessage DelSpareElevator(int id)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            string result = string.Empty;
            try
            {
                result = da.DelSpareElevator(id);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除设备绑定备件关系";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("删除设备绑定备件关系" + e.ToString());
            }
            return message;
        }

        public ResponseMessage QuerySpareElevator(int elevatorid)
        {
            ResponseMessage message = new ResponseMessage();
            SpareDAL da = new SpareDAL();
            try
            {
                List<SpareElevator> content = da.QuerySpareElevator(elevatorid);
                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询设备绑定备件关系";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询设备绑定备件关系" + e.ToString());
            }

            return message;
        }

        public ResponseMessage ExportSpareRecordHistory(Spareupdaterecord spareupdaterecord, string path)
        {
            ResponseMessage response = new ResponseMessage();
            try
            {
                SpareDAL da = new SpareDAL();
                List<Spareupdaterecord> list = new List<Spareupdaterecord>();
                if (!string.IsNullOrEmpty(spareupdaterecord.ids))
                {
                    list = da.QuerySpareRecordByIds(spareupdaterecord.ids);
                }
                else
                {
                    list = da.QuerySpareRecord(spareupdaterecord);
                }
                if (list.Count>0)
                {
                    if (list.Count < 65536)
                    {
                        string sFileName = "备件更换记录.xlsx";

                        FileInfo file = new FileInfo(Path.Combine(path, sFileName));

                        file.Delete();
                        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                        using (ExcelPackage package = new ExcelPackage(file))

                        {
                            // 添加worksheet

                            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("备件更换记录");

                            //添加头
                            worksheet.Cells[1, 1].Value = "部件名称";
                            worksheet.Cells[1, 2].Value = "规格型号";
                            worksheet.Cells[1, 3].Value = "数量";
                            worksheet.Cells[1, 4].Value = "更换原因";
                            worksheet.Cells[1, 5].Value = "更换时间";
                            worksheet.Cells[1, 6].Value = "替换件名称";
                            worksheet.Cells[1, 7].Value = "替换件规格型号";

                            //添加值

                            for (int i = 0; i < list.Count; i++)
                            {
                                Spareupdaterecord spareupdaterecord1 = list[i];
                                worksheet.Cells[i + 2, 1].Value = spareupdaterecord1.oldSpareName;
                                worksheet.Cells[i + 2, 2].Value = spareupdaterecord1.oldSpareModel;
                                worksheet.Cells[i + 2, 3].Value = spareupdaterecord1.amount;
                                worksheet.Cells[i + 2, 4].Value = spareupdaterecord1.updateReason;
                                worksheet.Cells[i + 2, 5].Value = spareupdaterecord1.updateDate == null ? "" : Convert.ToDateTime(spareupdaterecord1.updateDate).ToString("yyyy/MM/dd HH:mm:ss");
                                worksheet.Cells[i + 2, 6].Value = spareupdaterecord1.spareName;
                                worksheet.Cells[i + 2, 7].Value = spareupdaterecord1.spareModel;
                            }
                            package.Save();

                        }
                        response.errcode = 0;
                        response.errmsg = "";
                        response.data = "/" + sFileName;
                    }
                    else
                    {
                        response.errcode = 1;
                        response.errmsg = "数据量超过65535条，无法导出";
                    }
                }
                else
                {
                    response.errcode = 1;
                    response.errmsg = "无导出数据";
                }
            }
            catch (Exception ex)
            {
                response.errcode = 1;
                response.errmsg = "导出失败";
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出备件更换记录";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("导出备件更换记录" + ex.ToString());
            }
            return response;
        }
    }
}
